package com.nq.controller.backend;


import com.nq.common.ServerResponse;

import com.nq.dao.MyDao;
import com.nq.pojo.UserPosition;
import com.nq.pojo.UserRecharge;
import com.nq.pojo.vo.StFundDetailsVo;
import com.nq.pojo.vo.StPositionDetailsVo;
import com.nq.service.IUserPositionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Map;


@Controller
@RequestMapping({"/admin/position/"})
public class AdminPositionController {
    private static final Logger log = LoggerFactory.getLogger(AdminPositionController.class);

    @Autowired
    IUserPositionService iUserPositionService;

    @Resource
    SqlSessionTemplate sqlSessionTemplate;

    /**
     * 分页查询持仓管理 融资持仓单信息/融资平仓单信息及模糊查询
     * @param agentId
     * @param positionType
     * @param state
     * @param userId
     * @param positionSn
     * @param beginTime
     * @param endTime
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping({"list.do"})
    @ResponseBody
    public ServerResponse list(@RequestParam(value = "agentId", required = false) Integer agentId,
                               @RequestParam(value = "positionType", required = false) Integer positionType,
                               @RequestParam(value = "state", required = false) Integer state,
                               @RequestParam(value = "userId", required = false) Integer userId,
                               @RequestParam(value = "stockCode", required = false) String stockCode,
                               @RequestParam(value = "positionSn", required = false) String positionSn,
                               @RequestParam(value = "userPhone", required = false) String userPhone,
                               @RequestParam(value = "beginTime", required = false) String beginTime,
                               @RequestParam(value = "endTime", required = false) String endTime,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "12") int pageSize,
                               HttpServletRequest request) {
        return this.iUserPositionService.listByAdmin(agentId, positionType, state,
                userId, stockCode, positionSn, userPhone, beginTime, endTime, pageNum, pageSize);
    }

    @RequestMapping({"update.do"})
    @ResponseBody
    public Object update(@RequestParam Map<String, Object> position) {
        boolean flag = MyDao.updateById("user_position", position);
        if (flag) {
            return ServerResponse.createBySuccess("操作成功");
        }
        return ServerResponse.createByErrorMsg("操作失败");
    }

    //持仓管理 强制平仓操作
    @RequestMapping({"sell.do"})
    @ResponseBody
    public ServerResponse sell(String positionSn) throws Exception {
        ServerResponse serverResponse = null;
        try {
            serverResponse = this.iUserPositionService.sell(positionSn, 0);
        } catch (Exception e) {
            log.error("Forced liquidation abnormal information = {}", e);
        }
        return serverResponse;
    }

    //锁仓/解仓操作
    @RequestMapping({"lock.do"})
    @ResponseBody
    public ServerResponse lock(@RequestParam("positionId") Integer positionId, @RequestParam("state") Integer state, @RequestParam(value = "lockMsg", required = false) String lockMsg) {
        return this.iUserPositionService.lock(positionId, state, lockMsg);
    }

    @RequestMapping({"del.do"})
    @ResponseBody
    public ServerResponse del(@RequestParam("positionId") Integer positionId) {
        return this.iUserPositionService.del(positionId);
    }

    //创建持仓单
    @RequestMapping({"create.do"})
    @ResponseBody
    public ServerResponse create(@RequestParam("userId") Integer userId,
                                 @RequestParam("stockCode") String stockCode,
                                 @RequestParam("buyPrice") String buyPrice,
                                 @RequestParam("buyTime") String buyTime,
                                 @RequestParam("buyNum") Integer buyNum,
                                 @RequestParam("buyType") Integer buyType,
                                 @RequestParam("lever") Integer lever,
                                 @RequestParam(value = "profitTarget", required = false) BigDecimal profitTarget,
                                 @RequestParam(value = "stopTarget", required = false) BigDecimal stopTarget) {
        return this.iUserPositionService.create(userId, stockCode, "", buyPrice, buyTime, buyNum, buyType, lever, profitTarget, stopTarget);
    }

    //新股转持仓
    @RequestMapping({"newStockToPosition.do"})
    @ResponseBody
    public ServerResponse newStockToPosition(@RequestParam("id") Integer id) {
        return this.iUserPositionService.newStockToPosition(id, null);
    }

    /**
     * 查询订单交易详情
     * @return
     */
    @RequestMapping({"queryPositionDetails.do"})
    @ResponseBody
    public ServerResponse queryPositionDetails(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                               @RequestParam(value = "userId", required = false) Integer userId,
                                               @RequestParam(value = "phone", required = false) String phone,
                                               @RequestParam(value = "positionNo", required = false) String positionNo,
                                               @RequestParam(value = "startAmount", required = false) String startAmount
    ) {
        return this.iUserPositionService.queryPositionDetails(phone, userId, positionNo, startAmount, pageNum, pageSize);
    }

    /**
     * 查询基金订单交易详情
     * @return
     */
    @RequestMapping({"queryFundApplyDetails.do"})
    @ResponseBody
    public ServerResponse queryFundApplyDetails(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                               @RequestParam(value = "userId", required = false) Integer userId,
                                               @RequestParam(value = "orderNumber", required = false) String orderNumber,
                                               @RequestParam(value = "phone", required = false) String phone,
                                                @RequestParam(value = "startAmount", required = false) String startAmount) {
        return this.iUserPositionService.queryFundApplyDetails(phone, userId, orderNumber, startAmount, pageNum, pageSize);
    }


    /**
     * 编辑订单交易详情
     * @return
     */
    @RequestMapping({"updateDetails.do"})
    @ResponseBody
    public ServerResponse updateDetails(StPositionDetailsVo vo) {
        return this.iUserPositionService.updateDetails(vo);
    }

    /**
     * 编辑基金订单交易详情
     * @return
     */
    @RequestMapping({"editFundDetails.do"})
    @ResponseBody
    public ServerResponse editFundDetails(StFundDetailsVo vo) {
        return this.iUserPositionService.editFundDetails(vo);
    }

}

